package com.xjmall.controller.background;

import com.github.pagehelper.PageInfo;
import com.xjmall.common.Const;
import com.xjmall.common.ServerResult;
import com.xjmall.exception.AuthorityException;
import com.xjmall.exception.NotLoginException;
import com.xjmall.pojo.User;
import com.xjmall.service.IOrderService;
import com.xjmall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 后台订单管理Controller
 * Created by An on 2017/7/27.
 */
@Controller
@RequestMapping("/manage/order/")
public class OrderManageController {

    @Autowired
    private IOrderService iOrderService;

    /**
     * 未支付订单列表
     * @param session 当前会话
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return ServerResult
     */
    @RequestMapping(value = "no_pay_list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<PageInfo> nopayOrderList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageNopayList(pageNum, pageSize);
    }

    /**
     * 未发货订单列表
     * @param session 当前会话
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return ServerResult
     */
    @RequestMapping(value = "no_shipped_list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<PageInfo> noShippedOrderList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageNoShippedList(pageNum, pageSize);
    }

    /**
     * 完成交易订单列表
     * @param session 当前会话
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return ServerResult
     */
    @RequestMapping(value = "order_success_list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<PageInfo> orderSuccessList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageOrderSuccessList(pageNum, pageSize);
    }

    /**
     * 交易关闭订单列表
     * @param session 当前会话
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return 通用服务器响应对象
     */
    @RequestMapping(value = "order_close_list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult<PageInfo> orderCloseList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageOrderCloseList(pageNum, pageSize);
    }

    /**
     * 订单改价
     * @param session 当前会话
     * @param orderNo 订单id
     * @param editPayment 交易价格
     * @return ServerResult
     */
    @RequestMapping(value = "edit_actual_payment.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResult editActualPayment(HttpSession session, Long orderNo, Double editPayment) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageEditActualPayment(orderNo, editPayment);
    }

    /**
     * 订单发货
     * @param session 当前会话
     * @param orderNo 订单号
     * @return ServerResult
     */
    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServerResult sendGoods(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageSendGoods(orderNo);
    }

    /**
     * 获取订单详情
     * @param session 当前会话
     * @param orderNo 订单号
     * @return 通用服务器响应对象
     */
    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResult<OrderVo> detail(HttpSession session, Long orderNo) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageDetail(orderNo);
    }

    /**
     * 订单搜索
     * @param session 当前会话
     * @param orderNo 订单号
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return ServerResult
     */
    @RequestMapping("search.do")
    @ResponseBody
    public ServerResult<PageInfo> search(HttpSession session, Long orderNo, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            throw new NotLoginException("用户未登录，拒绝访问");
        }
        // 校验用户是否为管理员
        if (user.getRole() != Const.Role.ROLE_ADMIN) {
            throw new AuthorityException("无操作权限");
        }
        // 调用业务层接口执行操作
        return iOrderService.manageSearch(orderNo, pageNum, pageSize);
    }
}